In [25]:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.font_manager as fm
import seaborn as sns
In [26]:
df = pd.read_csv('./importance.csv')
df.head()
Out[26]:
| Feature | Importance | |
|---|---|---|
| 0 | Age | 100.000000 |
| 1 | MCV | 89.158979 |
| 2 | RDW | 67.933354 |
| 3 | Acute Respiratory Failure | 66.976096 |
| 4 | Respiratory Rate | 63.433521 |
In [3]:
# 检查系统上是否安装了 Times New Roman
font_properties = fm.FontProperties(fname=fm.findSystemFonts(fontpaths=None, fontext='ttf')[0])
print(font_properties.get_name())
# 设置字体
plt.rc('font', family='Times New Roman')
# 使用 seaborn 绘制条形图
plt.figure(figsize=(16, 10), dpi=900)
blue_palette = list(reversed(sns.color_palette("Blues", n_colors=len(df))))
bar_plot = sns.barplot(x='Importance',
y='Feature',
data=df,
orient='h',
palette=blue_palette)
# 在条形图上方显示值
for i, bar in enumerate(bar_plot.patches):
# 获取条形的宽度(数值)
bar_width = bar.get_width()
# 获取条形的 y 位置
bar_y = bar.get_y() + bar.get_height() / 2
# 在条形右侧稍作偏移的位置显示数值
bar_plot.text(bar_width + 0.5, bar_y, round(bar_width, 2), va='center')
# plt.show()
plt.savefig('./importance.png')
Microsoft YaHei
C:\Users\kexu\AppData\Local\Temp\ipykernel_19228\4193398195.py:13: FutureWarning: Passing `palette` without assigning `hue` is deprecated and will be removed in v0.14.0. Assign the `y` variable to `hue` and set `legend=False` for the same effect. bar_plot = sns.barplot(x='Importance',
In [30]:
# 检查系统上是否安装了 Times New Roman
font_properties = fm.FontProperties(fname=fm.findSystemFonts(fontpaths=None, fontext='ttf')[0])
print(font_properties.get_name())
# 设置字体
plt.rc('font', family='Times New Roman')
# 使用 seaborn 绘制条形图
plt.figure(figsize=(16, 6), dpi=900)
blue_palette = list(reversed(sns.color_palette("Blues", n_colors=len(df))))
bar_plot = sns.barplot(
x='Feature',
y='Importance',
data=df,
orient='v',
palette=blue_palette,
)
for i, bar in enumerate(bar_plot.patches):
bar_height = bar.get_height()
if bar_height <= 0: continue
bar_x = bar.get_x() + bar.get_width() / 2
bar_plot.text(bar_x, bar_height+1.5, round(bar_height, 2), ha='center')
# 去除上方和右侧的框线
ax = plt.gca()
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
plt.xticks(rotation=20, ha='right')
plt.tight_layout()
plt.savefig('./importance.png')
Microsoft YaHei
C:\Users\kexu\AppData\Local\Temp\ipykernel_19228\306420801.py:13: FutureWarning: Passing `palette` without assigning `hue` is deprecated and will be removed in v0.14.0. Assign the `x` variable to `hue` and set `legend=False` for the same effect. bar_plot = sns.barplot(
In [ ]: